package com.xinan.dmgl.cggl;

import com.jfinal.aop.Before;
import com.jfinal.core.BaseController;
import com.jfinal.core.Const;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.SearchSql;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.xinan.utils.Constants;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.render.JsonRender;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CpglRkController extends BaseController {

	public void index() {
		render("/WEB-INF/views/cggl/wprk/cpglRk.html");
	}

	/**
	 * add
	 */
	public void newModes() {
		String id = getPara("id");
		// List<Record> newlist = Db.find("select  * from     where    id=  '" +
		// id + "'");
		List<Record> newlist = CpglRk.dao.findAlls("       id=  '" + id + "'");
		List<Record> cklist = CpglCkgl.dao.findAlls("  yxbz=1   ");
		setAttr("newlist", newlist);
		setAttr("cklist", cklist);
		render("/WEB-INF/views/cggl/wprk/cpglRk_form.html");
	}

	/**
	 * 退货
	 */
	public void thsave() {
		String id = getPara("id");
		String wpsl=getPara("wpsl");
		String wpid=getPara("wpid");
		String cksl=getPara("cksl");
		boolean cont=getModel(CpglRk.class).set("wpsl",wpsl).remove("wpid").update();
		Record xx= new Record();
		xx.set("wpid",wpid).set("cksl",cksl).set("ckyy", "2");//2退货出库
		boolean conts=Db.save("cpgl_ck", xx);
		if(cont&&conts){
			render(new JsonRender("1").forIE());
		} else {
			render(new JsonRender("0").forIE());
		}
	}
	/**
	 * 退货
	 */
	public void removeModes() {
		String id = getPara("id");
		List<Record> newlist = CpglRk.dao.findAlls("       id=  '" + id + "'");
		setAttr("newlist", newlist);
		setAttr("rkid", id);
		render("/WEB-INF/views/cggl/wprk/rkth.html");
	}
	/**
	 * 在增加了SearchSql拦截之后就可以根据页面配置信息
	 * 自动装配查询语句放入request中,通过getAttr(Const.SEARCH_SQL)函数获得 根据实际需要自动组装到Sql语句里面
	 */
	@Before(SearchSql.class)
	public void query() {
		int pageNumber = getParaToInt("page");
		int pageSize = getParaToInt("rows");
		String where = getAttr(Const.SEARCH_SQL);
		Page<CpglRk> page = CpglRk.dao.page(pageNumber, pageSize, where);
		Map root = new HashMap();
		// / jqgrid用 分页显示
		root.put("total", page.getTotalPage());
		root.put("page", page.getPageNumber());
		root.put("records", page.getTotalRow());
		root.put("rows", page.getList());
		renderJson(JsonKit.toJson(root));
	}

	/**
	 * save
	 * 
	 * @param
	 * @return
	 */
	@Before(Tx.class)
	public void save() {
		String llrydm = getAttr(Constants.USER_NAME);
		String lrrq = "";
		Date dt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		lrrq = sdf.format(dt);
		String wpid = getPara("wpid");
		String wpsl = getPara("wpsl");
		long kc = 0;
		List<Record> kcsl = Db.find("select kcsl from cggl_wp  where id= '"
				+ wpid + "'");
		kc = Long.parseLong(kcsl.get(0).get("kcsl") == null ? "0"
				: (String) kcsl.get(0).get("kcsl")) + Long.parseLong(wpsl);
		Db.update("update cggl_wp set kcsl=" + kc + "  where id= '" + wpid
				+ "'");
		if (getModel(CpglRk.class).set("lrrq", lrrq).set("llrydm", llrydm)
				.save()) {
			// renderJson(Constants.SAVESUCCESS);
			render(new JsonRender(Constants.SAVESUCCESS).forIE());

		} else {
			// renderJson(Constants.SAVEFAILED);
			render(new JsonRender(Constants.SAVEFAILED).forIE());

		}
	}

	/**
	 * Post请求的菜单更新函数,可以接受菜单的id和菜单对象,并且根据菜单id进行更新,返回影响行数根据此判断是否操作成功
	 * 本例子使用rose框架的est风格url可以吧参数作为路径一部分进行传递
	 * 
	 * @param
	 * @return
	 */
	public void update() {
		if (getModel(CpglRk.class).update()) {
			// renderJson(Constants.EDITSUCCESS);
			render(new JsonRender(Constants.EDITSUCCESS).forIE());

		} else {
			// renderJson(Constants.EDITFAILED);
			render(new JsonRender(Constants.EDITFAILED).forIE());

		}
	}

	/**
	 * Post请求的菜单删除函数,可以接受菜单ID 并且根据ID进行物理删除,并且返回影响行数,据此判断是否操作成功
	 * 
	 * @param
	 * @return
	 */
	@Before(Tx.class)
	public void delete() {
		String wpid = getPara("wpid");
		String wpsl = getPara("wpsl");
		long kc = 0;
		List<Record> kcsl = Db.find("select kcsl from cggl_wp  where id= '"
				+ wpid + "'");
		kc = Long.parseLong(kcsl.get(0).get("kcsl") == null ? "0"
				: (String) kcsl.get(0).get("kcsl")) - Long.parseLong(wpsl);
		Db.update("update cggl_wp set kcsl=" + kc + "  where id= '" + wpid
				+ "'");
		if (CpglRk.dao.deleteById(getPara("id"))) {
			// renderJson(Constants.DELETESUCCESS);
			render(new JsonRender(Constants.DELETESUCCESS).forIE());

		} else {
			// renderJson(Constants.DELETEFAILED);
			render(new JsonRender(Constants.DELETEFAILED).forIE());

		}
	}
}
